package xmluserdata;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.axis.utils.XMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.ByteArrayInputStream;



public class worker
{
  
  private static final String PRIMARYDEALER   = "primaryDealer";
  private static final String SECONDARYDEALER   = "secondaryDealer";
  
  
  
  public static void main(String args[]) throws Exception
  {
    String ntlXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <ssores:ssoResponse xmlns:ssores=\"http://sso.honda.ca\"><userProfile><email value=\"sira_carrion@ch.honda.com\"/><firstName value=\"MCPE\"/><lastName value=\"BSSU\"/><division value=\"\"/><department value=\"\"/><title value=\"\"/><scope value=\"N\"/><zoneCode errorCode=\"231\"/><studentId value=\"D0023624\"/><dealers><primaryDealer><dealerCode value=\"00000\"/><dealerName value=\"Honda Canada Inc\"/><productLines><productLine value=\"C\"/><productLine value=\"A\"/><productLine value=\"M\"/><productLine value=\"P\"/></productLines><zoneCode errorCode=\"230\"/><dealerAddress errorCode=\"400\"/><hiSpeed value=\"Y\"/><calibre/></primaryDealer></dealers></userProfile></ssores:ssoResponse>";
    String phdXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <ssores:ssoResponse xmlns:ssores=\"http://sso.honda.ca\"><userProfile><email value=\"1@a.com\"/><firstName value=\"3114\"/><lastName value=\"minacs\"/><division value=\"\"/><department value=\"\"/><title value=\"\"/><scope value=\"D\"/><zoneCode errorCode=\"230\"/><studentId errorCode=\"321\"/><dealers><primaryDealer><dealerCode value=\"3114\"/><dealerName value=\"YELLOWHEAD HONDA\"/><productLines><productLine value=\"A\"/></productLines><zoneCode value=\"1\"/><dealerAddress><addressLine lineId=\"1\" value=\"#17860 - 102ND AVE.\"/><addressLine errorCode=\"401\" lineId=\"2\" value=\"bobo\"/><postalCode value=\"T5S1M9\"/><provinceCode value=\"A\"/></dealerAddress><hiSpeed value=\"N\"/><calibre value=\"20\"/></primaryDealer><secondaryDealer><dealerCode value=\"3445\"/><dealerName value=\"POINTE WEST HONDA POWERHOUSE\"/><productLines><productLine value=\"P\"/><productLine value=\"QW\"/></productLines><zoneCode value=\"2\"/><dealerAddress><addressLine lineId=\"1\" value=\"17860-102ND AVE\"/><addressLine errorCode=\"401\" lineId=\"2\"/><postalCode value=\"T5S1M9\"/><provinceCode value=\"A\"/></dealerAddress><hiSpeed value=\"N\"/><calibre/></secondaryDealer></dealers></userProfile></ssores:ssoResponse>";
    String hndXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <ssores:ssoResponse xmlns:ssores=\"http://sso.honda.ca\"><userProfile><email value=\"1@a.com\"/><firstName value=\"6711\"/><lastName value=\"minacs\"/><division value=\"\"/><department value=\"\"/><title value=\"\"/><scope value=\"D\"/><zoneCode errorCode=\"230\"/><studentId errorCode=\"321\"/><dealers><primaryDealer><dealerCode value=\"6711\"/><dealerName value=\"FORMULA HONDA\"/><productLines><productLine value=\"A\"/></productLines><zoneCode value=\"5\"/><dealerAddress><addressLine lineId=\"1\" value=\"#2240 MARKHAM ROAD\"/><addressLine errorCode=\"401\" lineId=\"2\"/><postalCode value=\"M1B2W4\"/><provinceCode value=\"O\"/></dealerAddress><hiSpeed value=\"N\"/><calibre value=\"90\"/></primaryDealer></dealers></userProfile></ssores:ssoResponse>";
    String xml;
    try{
      if(args[0].equals("phd"))
        xml = phdXML;
      else if(args[0].equals("ntl"))
        xml = ntlXML;
      else
        xml = hndXML;
     }catch(Exception e)
     {
       System.out.println("Error assigning argument");
       System.out.println("\n");
       xml = hndXML;
     }
     
     //System.out.println(xml);
     worker w = new worker();
     UserData ud = w.getUserData(xml);
     System.out.println("email is set to: " + ud.email );
     System.out.println("firstName is set to: " + ud.firstName );
     System.out.println("lastName is set to: " + ud.lastName );
     System.out.println("division is set to: " + ud.division );
     System.out.println("department is set to: " + ud.department );
     System.out.println("title is set to: " + ud.title );
     System.out.println("scope is set to: " + ud.scope );
     System.out.println("zoneCode is set to: " + ud.zoneCode );
     System.out.println("studentId is set to: " + ud.studentId );
     
     System.out.println("\n");
     System.out.println("ud.dealerCode:" + ud.dealerCode);
     System.out.println("ud.dealerName:" + ud.dealerName);
     System.out.println("ud.hiSpeed:" + ud.hiSpeed);
     System.out.println("ud.dealerZoneCode:" + ud.dealerZoneCode);
     System.out.println("ud.dealerAddress1:" + ud.dealerAddress1);
     System.out.println("ud.dealerAddress2:" + ud.dealerAddress2);
     System.out.println("ud.productLines:" + ud.productLines);
     
     System.out.println("\n");
     System.out.println("ud.secondaryDealerCode:" + ud.secondaryDealerCode);
     System.out.println("ud.secondaryDealerName:" + ud.secondaryDealerName);
     System.out.println("ud.secondaryHiSpeed:" + ud.secondaryHiSpeed);
     System.out.println("ud.secondaryDealerZoneCode:" + ud.secondaryDealerZoneCode);
     System.out.println("ud.secondaryDealerAddress1:" + ud.secondaryDealerAddress1);
     System.out.println("ud.secondaryDealerAddress2:" + ud.secondaryDealerAddress2);
     System.out.println("ud.secondaryProductLines:" + ud.secondaryProductLines);
      
  }
  
  public UserData getUserData(String s) throws Exception
  {
    ByteArrayInputStream bs = new ByteArrayInputStream(s.getBytes());
    
    UserData ud = new UserData();
    
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document resultDoc = builder.parse(bs);
    Element firstElement = resultDoc.getDocumentElement(); 
    NodeList root = firstElement.getChildNodes();
    for (int rootIdx = 0; rootIdx < root.getLength(); rootIdx++)
    {
      Node rootNode = (Node) root.item(rootIdx);
      if (rootNode.getNodeName().equalsIgnoreCase("userProfile"))
      {
        NodeList UserList = rootNode.getChildNodes();
        
        for (int i = 0; i < UserList.getLength(); i++)
        {
          Element e = (Element) UserList.item(i);
          System.out.println("" + e.getTagName());
          
          
          if(e.getTagName().equals("email") & e.hasAttribute("value"))
            ud.email = e.getAttribute("value");
          
          if(e.getTagName().equals("firstName") & e.hasAttribute("value"))
            ud.firstName = e.getAttribute("value");
            
          if(e.getTagName().equals("lastName") & e.hasAttribute("value"))
            ud.lastName = e.getAttribute("value");
            
          if(e.getTagName().equals("division") & e.hasAttribute("value"))
            ud.division = e.getAttribute("value");
            
          if(e.getTagName().equals("department") & e.hasAttribute("value"))
            ud.department = e.getAttribute("value");
            
          if(e.getTagName().equals("title") & e.hasAttribute("value"))
            ud.title = e.getAttribute("value");
            
          if(e.getTagName().equals("scope") & e.hasAttribute("value"))
            ud.scope = e.getAttribute("value");
            
          if(e.getTagName().equals("zoneCode") & e.hasAttribute("value"))
            ud.zoneCode = e.getAttribute("value");
            
          if(e.getTagName().equals("studentId") & e.hasAttribute("value"))
            ud.studentId = e.getAttribute("value");
            
          if(e.getTagName().equals("dealers"))
          {
             
            Node dNode = (Node) e;
            NodeList dNodeList	= dNode.getChildNodes();
             
            for (int x=0;x < dNodeList.getLength();x++)
            {
              
              Node n = (Node) dNodeList.item(x);
               
              if(n.getNodeName().equals(PRIMARYDEALER))
              {
                buildDealerData(ud, n);
              }
               
              if(n.getNodeName().equals(SECONDARYDEALER))
              {
                buildDealerData(ud, n, SECONDARYDEALER);
              }
            }// end loop of Dealers
          }// end if tag is dealers
        }// end loop through UserProfile node
      }// end if node is UserProfile
    }// end loop through root node
    
    return ud;
  }
  
  private void buildDealerData(UserData ud, Node n) throws Exception
  {
    buildDealerData(ud,n,PRIMARYDEALER);
  }
  
  private void buildDealerData(UserData ud, Node n, String applyToCode) throws Exception
  {
    NodeList list	= n.getChildNodes();
    for (int i=0;i < list.getLength();i++)
    {
      Element e = (Element) list.item(i);
                    
      if(e.getTagName().equals("dealerCode") && e.hasAttribute("value"))
      {
        if(applyToCode.equals(SECONDARYDEALER))
          ud.secondaryDealerCode = e.getAttribute("value");
        else
          ud.dealerCode = e.getAttribute("value");
      }
        
        
      if(e.getTagName().equals("dealerName") && e.hasAttribute("value"))
      {
        if(applyToCode.equals(SECONDARYDEALER))
          ud.secondaryDealerName = e.getAttribute("value");
        else
          ud.dealerName = e.getAttribute("value");
      }
        
        
      if(e.getTagName().equals("zoneCode") && e.hasAttribute("value"))
      {
        if(applyToCode.equals(SECONDARYDEALER))
          ud.secondaryDealerZoneCode = e.getAttribute("value");
        else
          ud.dealerZoneCode = e.getAttribute("value");
      }
         
        
      if(e.getTagName().equals("hiSpeed") && e.hasAttribute("value"))
      {
        if(applyToCode.equals(SECONDARYDEALER))
          ud.secondaryHiSpeed = e.getAttribute("value");
        else
          ud.hiSpeed = e.getAttribute("value");
      }
      
      if(e.getTagName().equals("dealerAddress"))
      {
        Node addr = (Node) e;
        NodeList addrList	= addr.getChildNodes();
        for(int x=0;x<addrList.getLength();x++)
        {
          Element addrE = (Element) addrList.item(x);
          //System.out.println("    address Information: " + addrE.hasAttribute("lineId"));
          
          if(addrE.hasAttribute("lineId") && addrE.getAttribute("lineId").equals("1") && addrE.hasAttribute("value"))
          {
            if(applyToCode.equals(SECONDARYDEALER))
              ud.secondaryDealerAddress1 = addrE.getAttribute("value");
            else
              ud.dealerAddress1 = addrE.getAttribute("value");
          }
          
          if(addrE.hasAttribute("lineId") && addrE.getAttribute("lineId").equals("2") && addrE.hasAttribute("value"))
          {
            if(applyToCode.equals(SECONDARYDEALER))
              ud.secondaryDealerAddress2 = addrE.getAttribute("value");
            else
              ud.dealerAddress2 = addrE.getAttribute("value");
          }
        }// end looping through dealerAddress Nodes
      }// end if node is a DealerAddress
        
      
      if(e.getTagName().equals("productLines"))
      {
        String prodLines="";
        Node prods = (Node) e;
        NodeList pList	= prods.getChildNodes();
        for(int x=0;x<pList.getLength();x++)
        {
          Element prodE = (Element) pList.item(x);
          if(prodE.hasAttribute("value"))
          {
            if(x>0) 
              prodLines = prodLines + ",";
              
            prodLines = prodLines + prodE.getAttribute("value");
          }
            
        }// end loop through Product lines
        
        if(applyToCode.equals(SECONDARYDEALER))
          ud.secondaryProductLines = prodLines;
        else
          ud.productLines = prodLines;
      }// end if node contains productLines
    }// end looping top node 
  }// end buildDealerData
}